#include <stdio.h>

#define COL8_000000 0
#define COL8_FF0000 1
#define COL8_00FF00 2
#define COL8_FFFF00 3
#define COL8_0000FF 4
#define COL8_FF00FF 5
#define COL8_00FFFF 6
#define COL8_FFFFFF 7
#define COL8_C6C6C6 8
#define COL8_840000 9
#define COL8_008400 10
#define COL8_848400 11
#define COL8_000084 12
#define COL8_840084 13
#define COL8_008484 14
#define COL8_848484 15

/**
 * 妙啊！
 * binfo = (struct BOOTINFO *)0x0ff0;
 * 以0x0ff0作为结构体地址，对应asmhead.nas的cyls，也就是最开始的地址
 * 后面的每一个属性就可以直接对应地址了
 */
struct BOOTINFO
{
	char cyls, leds, vmode, reserve;
	short scrnx, scrny;
	char *vram;
};

/* GDT global (segment) descriptor table 和 IDT interrupt descriptor table */
/* 全局段号记录表 和 中断记录表 */
/* 存放GDT的8字节的内容 */
struct SEGMENT_DESCRIPTOR
{
	short limit_low, base_low;
	char base_mid, access_right;
	char limit_high, base_high;
};

/* 存放IDT的8字节的内容 */
struct GATE_DESCRIPTOR
{
	short offset_low, selector;
	char dw_count, access_right;
	short offset_high;
};

void io_hlt(void);
void io_cli(void);
void io_out8(int port, int data);
int io_load_eflags(void);
void io_store_eflags(int eflags);

/* 就算写在同一个源文件里，如果想在定义前使用，还是必须实现声明一下。 */

void init_palette(void); /* 调色板 */
void set_palette(int start, int end, unsigned char *rgb);
void boxfill8(unsigned char *vram, int xsize, unsigned char c, int x0, int y0, int x1, int y1);
void init_screen8(int *vram, int x, int y);
void putfont8(char *vram, int xsize, int x, int y, char c, char *font); /* 画字 */
void putfont8_asc(char *vram, int xsize, int x, int y, char c, unsigned char *s);
void init_mouse_cursor8(char *mouse, char bc); /* 准备鼠标指针 */
void putblock8_8(char *vram, int vxsize, int pxsize, int pysize,
				 int px0, int py0, char *buf, int bxsize);
void init_gdtidt(void); /* 初始化GDT和IDT */
void set_segmdesc(struct SEGMENT_DESCRIPTOR *sd, unsigned int limit, int base, int ar);
void set_gatedesc(struct GATE_DESCRIPTOR *gd, int offset, int selector, int ar);
void load_gdtr(int limit, int addr);
void load_idtr(int limit, int addr);

void HariMain(void)
{
	struct BOOTINFO *binfo = (struct BOOTINFO *)0x0ff0;
	char s[40], mcursor[256];
	int mx, my;

	init_gdtidt();
	init_palette();
	init_screen8(binfo->vram, binfo->scrnx, binfo->scrny);
	mx = (binfo->scrnx - 16) / 2; /* 屏幕中心坐标计算 */
	my = (binfo->scrny - 28 - 16) / 2;
	init_mouse_cursor8(mcursor, COL8_008484);
	putblock8_8(binfo->vram, binfo->scrnx, 16, 16, mx, my, mcursor, 16);
	sprintf(s, "(%d, %d)", mx, my);
	putfont8_asc(binfo->vram, binfo->scrnx, 0, 0, COL8_FFFFFF, s);

	for (;;)
	{
		io_hlt();
	}
}

void init_palette(void)
{
	/* 8位正好一个字节，也就是一个char */
	static unsigned char table_rgb[16 * 3] = {
		0x00, 0x00, 0x00, /* 0：黑 */
		0xff, 0x00, 0x00, /* 1：亮红 */
		0x00, 0xff, 0x00, /* 2：亮绿 */
		0xff, 0xff, 0x00, /* 3：亮黄 */
		0x00, 0x00, 0xff, /* 4：亮蓝 */
		0xff, 0x00, 0xff, /* 5：亮紫 */
		0x00, 0xff, 0xff, /* 6：浅亮蓝 */
		0xff, 0xff, 0xff, /* 7：白 */
		0xc6, 0xc6, 0xc6, /* 8：亮灰 */
		0x84, 0x00, 0x00, /* 9：暗红 */
		0x00, 0x84, 0x00, /* 10：暗绿 */
		0x84, 0x84, 0x00, /* 11：暗黄 */
		0x00, 0x00, 0x84, /* 12：暗青 */
		0x84, 0x00, 0x84, /* 13：暗紫 */
		0x00, 0x84, 0x84, /* 14：浅暗蓝 */
		0x84, 0x84, 0x84  /* 15：暗灰 */
	};
	set_palette(0, 15, table_rgb);
	return;

	/* C语言中的static char语句只能用于数据，相当于汇编中的DB指令 */
}

void set_palette(int start, int end, unsigned char *rgb)
{
	int i, eflags;
	eflags = io_load_eflags(); /* 记录中断许可标志的值 */
	io_cli();				   /* 将中断许可标志置为0，禁止中断 */
	io_out8(0x03c8, start);
	for (i = start; i <= end; i++)
	{
		io_out8(0x03c9, rgb[0] / 4);
		io_out8(0x03c9, rgb[1] / 4);
		io_out8(0x03c9, rgb[2] / 4);
		rgb += 3;
	}
	io_store_eflags(eflags); /* 复原中断许可标志 */
	return;
}

/**
 * boxfill8
 * vram：显存指针
 * xsize：屏幕宽度
 * c：色号
 * x0,y0：起始坐标
 * x1,y1：终点坐标
 */
void boxfill8(unsigned char *vram, int xsize, unsigned char c, int x0, int y0, int x1, int y1)
{
	int x, y;
	for (y = y0; y <= y1; y++)
	{
		for (x = x0; x <= x1; x++)
			vram[y * xsize + x] = c; /* VRAM地址计算：0xa0000 + x + y * 320（屏幕宽度为320） */
	}
	return;
}

void init_screen8(int *vram, int x, int y)
{
	/* 桌面、任务栏外框及阴影 */
	boxfill8(vram, x, COL8_008484, 0, 0, x - 1, y - 29);
	boxfill8(vram, x, COL8_C6C6C6, 0, y - 28, x - 1, y - 28);
	boxfill8(vram, x, COL8_FFFFFF, 0, y - 27, x - 1, y - 27);
	boxfill8(vram, x, COL8_C6C6C6, 0, y - 26, x - 1, y - 1);

	/* 左按钮外框及阴影 */
	boxfill8(vram, x, COL8_FFFFFF, 3, y - 24, 59, y - 24);
	boxfill8(vram, x, COL8_FFFFFF, 2, y - 24, 2, y - 4);
	boxfill8(vram, x, COL8_848484, 3, y - 4, 59, y - 4);
	boxfill8(vram, x, COL8_848484, 59, y - 23, 59, y - 5);
	boxfill8(vram, x, COL8_000000, 2, y - 3, 59, y - 3);
	boxfill8(vram, x, COL8_000000, 60, y - 24, 60, y - 3);

	/* 右下角小面板外框及阴影 */
	boxfill8(vram, x, COL8_848484, x - 47, y - 24, x - 4, y - 24);
	boxfill8(vram, x, COL8_848484, x - 47, y - 23, x - 47, y - 4);
	boxfill8(vram, x, COL8_FFFFFF, x - 47, y - 3, x - 4, y - 3);
	boxfill8(vram, x, COL8_FFFFFF, x - 3, y - 24, x - 3, y - 3);

	return;
}

/**
 * 每一段代码都是这么妙
 * 这里相当于逐行扫描
 * 循环16次，也就是16行
 * c：色号
 * font：字体形状数组
 */
void putfont8(char *vram, int xsize, int x, int y, char c, char *font)
{
	int i;
	char *p, d /* data */;
	for (i = 0; i < 16; i++)
	{
		p = vram + (y + i) * xsize + x;
		d = font[i];
		if ((d & 0x80) != 0)
		{
			p[0] = c;
		}
		if ((d & 0x40) != 0)
		{
			p[1] = c;
		}
		if ((d & 0x20) != 0)
		{
			p[2] = c;
		}
		if ((d & 0x10) != 0)
		{
			p[3] = c;
		}
		if ((d & 0x08) != 0)
		{
			p[4] = c;
		}
		if ((d & 0x04) != 0)
		{
			p[5] = c;
		}
		if ((d & 0x02) != 0)
		{
			p[6] = c;
		}
		if ((d & 0x01) != 0)
		{
			p[7] = c;
		}
	}
	return;
}

void putfont8_asc(char *vram, int xsize, int x, int y, char c, unsigned char *s)
{
	extern char hankaku[4096];
	for (; *s != 0x00; s++) /* C语言中字符串以0x00结尾 */
	{
		putfont8(vram, xsize, x, y, c, hankaku + *s * 16);
		x += 8;
	}
	return;
}

/* 
 * 准备鼠标指针
 * bc：背景色back-color
 */
void init_mouse_cursor8(char *mouse, char bc)
{
	static char cursor[16][16] = {
		"**************..",
		"*OOOOOOOOOOO*...",
		"*OOOOOOOOOO*....",
		"*OOOOOOOOO*.....",
		"*OOOOOOOO*......",
		"*OOOOOOO*.......",
		"*OOOOOOO*.......",
		"*OOOOOOOO*......",
		"*OOOO**OOO*.....",
		"*OOO*..*OOO*....",
		"*OO*....*OOO*...",
		"*O*......*OOO*..",
		"**........*OOO*.",
		"*..........*OOO*",
		"............*OO*",
		".............***"};
	int x, y;

	for (y = 0; y < 16; y++)
	{
		for (x = 0; x < 16; x++)
		{
			if (cursor[y][x] == '*')
			{
				mouse[y * 16 + x] = COL8_000000;
			}
			if (cursor[y][x] == 'O')
			{
				mouse[y * 16 + x] = COL8_FFFFFF;
			}
			if (cursor[y][x] == '.')
			{
				mouse[y * 16 + x] = bc;
			}
		}
	}
	return;
}

/**
 * vram和vxsize是关于VRAM的信息，值分别是0xa0000和320
 * pxsize和pysize（picture）是想要显示的图形的大小，鼠标指针是16x16，所以这两个值都是16
 * px0和py0指定图形的显示位置
 * buf和bxsize指定图形的存放地址和每一行含有的像素数
 * bxsize和pxsize大体相同，但有时可能需要分开放不同的值
 */
void putblock8_8(char *vram, int vxsize, int pxsize, int pysize,
				 int px0, int py0, char *buf, int bxsize)
{
	int x, y;
	for (y = 0; y < pysize; y++)
	{
		for (x = 0; x < pxsize; x++)
		{
			vram[(py0 + y) * vxsize + (px0 + x)] = buf[y * bxsize + x];
		}
	}
	return;
}

/**
 * 初始化GDT和IDT
 */
void init_gdtidt(void)
{
	struct SEGMENT_DESCRIPTOR *gdt = (struct SEGMENT_DESCRIPTOR *)0x00270000; /* 我们将0x270000 ~ 0x27ffff设为GDT（0xffff = 65535） */
	struct GATE_DESCRIPTOR *idt = (struct GATE_DESCRIPTOR *)0x0026f800;		  /* 将0x26f800 ~ 0x26ffff设为IDT */
	int i;

	/* GDT的初始化 */
	for (i = 0; i < 8192; i++)
	{
		/* 对所有段的设定 */
		/* 将上限limit（指段的字节数 - 1）、基址base、访问权限都设为0 */
		set_segmdesc(gdt + i, 0, 0, 0);
	}
	/* 单独对1和2号的两个段进行设定 */
	set_segmdesc(gdt + 1, 0xffffffff, 0x00000000, 0x4092); /* 上限为0xffffffff，即大小正好是4GB，地址是0 */
	set_segmdesc(gdt + 2, 0x0007ffff, 0x00280000, 0x409a); /* 上限为0x0007ffff，即512KB，地址是0x280000，这段地址正好是为bootpack.hrb准备的 */
	load_gdtr(0xffff, 0x00270000);

	/* IDT的初始化 */
	for (i = 0; i < 256; i++)
	{
		set_gatedesc(idt + i, 0, 0, 0);
	}
	load_idtr(0x7ff, 0x0026f800);

	return;
}

void set_segmdesc(struct SEGMENT_DESCRIPTOR *sd, unsigned int limit, int base, int ar)
{
	if (limit > 0xfffff)
	{
		ar |= 0x8000;	 /* ar = ar | 0x8000; 或（OR）运算符 G_bit = 1 */
		limit /= 0x1000; /* limit = limit / 0x1000; */
		sd->limit_low = limit & 0xffff;
		sd->base_low = base & 0xffff;
		sd->base_mid = (base >> 16) & 0xff; /* >>右移位运算符 00101100 >> 3等于00000101 */
		sd->access_right = ar & 0xff;
		sd->limit_high = ((limit >> 16) & 0x0f) | ((ar >> 8) & 0xf0);
		sd->base_high = (base >> 24) & 0xff;
		return;
	}
}

void set_gatedesc(struct GATE_DESCRIPTOR *gd, int offset, int selector, int ar)
{
	gd->offset_low = offset & 0xffff;
	gd->selector = selector;
	gd->dw_count = (ar >> 8) & 0xff;
	gd->access_right = ar & 0xff;
	gd->offset_high = (offset >> 16) & 0xffff;
	return;
}